package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.service.WxPayService;
import cn.itcast.utils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.util.Map;

/**
 * Demo class
 *
 * @author gyc
 * @date 2021/10/18
 */
@RestController
@RequestMapping("/pay")
public class WxPayController {

    @Autowired
    private WxPayService wxPayService;

    @GetMapping("/wxPay")
    public Result wxPay(@RequestParam String orderId,HttpServletRequest request){
        String code_url = wxPayService.sendQrCode(orderId,request);
        return new Result(true, "请求成功",code_url);
    }
    @RequestMapping("/notify")
    public String payNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
        InputStream inputStream = request.getInputStream();
        String strXml = ConvertUtils.convertToString(inputStream);
        Map<String, String> notifyMap = ConvertUtils.doXMLParse(strXml);
        return wxPayService.payBack(notifyMap);
    }

}
